package com.buildings.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.adapter.TaskPagerAdapter;
import com.buildings.base.BaseActivity;
import com.buildings.fragment.TaskListFragment;
import com.buildings.view.vpindicator.UnderlinePageIndicator;

import java.util.ArrayList;
import java.util.List;


/**
 * 任务列表
 */
public class TaskListActivity extends BaseActivity implements View.OnClickListener {

    TextView tvAll;
    TextView tvNoFinished;
    TextView tvFinished;
    UnderlinePageIndicator indicator;
    ViewPager viewPager;
    private List<Fragment> fragments;
    private TaskPagerAdapter pagerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_list);
        init();
    }

    private void init(){
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("任务列表");
        findViewById(R.id.iv_back).setOnClickListener(this);
        viewPager = findViewById(R.id.vp_task);
        tvAll = findViewById(R.id.tv_all);
        tvFinished = findViewById(R.id.tv_finished);
        tvNoFinished = findViewById(R.id.tv_no_finished);
        tvAll.setOnClickListener(this);
        tvFinished.setOnClickListener(this);
        tvNoFinished.setOnClickListener(this);
        indicator = findViewById(R.id.indicator);
        boolean isNet = getIntent().getBooleanExtra("isNet",false);
        fragments = new ArrayList<>();
        fragments.add(TaskListFragment.newInstance(""));//
        fragments.add(TaskListFragment.newInstance("1"));//
        fragments.add(TaskListFragment.newInstance("2"));//

        pagerAdapter = new TaskPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(4);
        invalidateTags(0);
        indicator.setViewPager(viewPager);
        indicator.setFades(false);
        indicator.setOnPageChangeListener(new MyOnPagerChangerListener());
    }

    private void invalidateTags(int position){
        switch (position){
            case 0:
                tvAll.setTextColor(getResources().getColor(R.color.blue_58));
                tvNoFinished.setTextColor(getResources().getColor(R.color.black));
                tvFinished.setTextColor(getResources().getColor(R.color.black));
                break;
            case 1:
                tvAll.setTextColor(getResources().getColor(R.color.black));
                tvNoFinished.setTextColor(getResources().getColor(R.color.blue_58));
                tvFinished.setTextColor(getResources().getColor(R.color.black));
                break;
            case 2:
                tvAll.setTextColor(getResources().getColor(R.color.black));
                tvNoFinished.setTextColor(getResources().getColor(R.color.black));
                tvFinished.setTextColor(getResources().getColor(R.color.blue_58));
                break;
        }
    }

    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_all:
                viewPager.setCurrentItem(0);
                break;
            case R.id.tv_no_finished:
                viewPager.setCurrentItem(1);
                break;
            case R.id.tv_finished:
                viewPager.setCurrentItem(2);
                break;

        }
    }

    private class MyOnPagerChangerListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            invalidateTags(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1000 && resultCode == RESULT_OK){
            refreshFragment(data);
        }
    }

    public void refreshFragment(Intent data){
//        for (int i=0; i<fragments.size(); i++){
//            PatientEducationFragment fragment = (PatientEducationFragment) fragments.get(i);
//            fragment.onRefresh(data);
//        }
    }
}
